package com.artist.entity;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class Log {
    private  Logger logger = Logger.getLogger(Log.class);
        /* logger.info("打印日志============INFO");
        logger.debug("打印日志============DEBUG");
        logger.warn("打印日志==============WARN");
        logger.error("打印日志=============ERROR");*/
    public void aftLog(){
        logger.info("打印日志============INFO");
    }


    public Object timeAround(ProceedingJoinPoint joinPoint) {
        // 定义返回对象、得到方法需要的参数
        Object obj = null;
        Object[] args = joinPoint.getArgs();
        long startTime = System.currentTimeMillis();

        try {
            obj = joinPoint.proceed(args);
        } catch (Throwable e) {
            logger.error("统计某方法执行耗时环绕通知出错", e);
        }

        // 获取执行的方法名
        long endTime = System.currentTimeMillis();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String methodName = signature.getDeclaringTypeName() + "." + signature.getName();
        // 打印耗时的信息
        this.printExecTime(methodName, startTime, endTime);
        return obj;
    }




public void printExecTime(String methodName, long startTime, long endTime) {
        long diffTime = endTime - startTime;
        //超过1秒的记录
        if (diffTime > 0) {
            logger.info(methodName + ":" + diffTime + " :ms");
        }

    }

}
